# Maintainer: Patrick Stewart <patstew@gmail.com>

#Not that this does supprot Python3 but I wanted to upgrade to 6.0x
#which does not support python2
_realname=jupyter_console
pkgbase=mingw-w64-python2-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=5.2.0
pkgrel=4
pkgdesc="A terminal-based console frontend for Jupyter kernels. (mingw-w64)"
arch=('any')
url="https://jupyter.org/"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-python2-jupyter_core"
         "${MINGW_PACKAGE_PREFIX}-python2-jupyter_client"
         "${MINGW_PACKAGE_PREFIX}-python2-colorama")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/jupyter/jupyter_console/archive/${pkgver}.tar.gz")
install=${_realname}2-${CARCH}.install
sha256sums=('371d03aeefcf48967f2f00af4c1709f52d2a688deee33f395c6330e4e8aa171c')

prepare() {
  cd "${srcdir}"
  rm -rf $"python2-build-${CARCH}" | true
  cp -r "${_realname}-${pkgver}" "python2-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
  export LC_CTYPE=en_US.UTF-8
}

build() {
    msg "Python 3 build for ${CARCH}"  
    cd "${srcdir}/python2-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python2 setup.py build
}

all_install() {
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/${1} setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=0 --skip-build

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package() {
  local interpreter=python2

  cd "${srcdir}/python2-build-${CARCH}"
  all_install ${interpreter}

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  cd "${pkgdir}${MINGW_PREFIX}"/bin
  for _f in *; do
    mv ${_f} ${_f/%.*/}2${_f/${_f/%.*/}/}
  done
}
